package com.capgemini.fs.app.ekms.service.implementations;

import com.capgemini.fs.app.ekms.model.Reply;
import com.capgemini.fs.app.ekms.model.Topic;
import com.capgemini.fs.app.ekms.model.User;
import com.capgemini.fs.app.ekms.persistence.PersistenceService;
import com.capgemini.fs.app.ekms.persistence.dao.ReplyDAO;
import com.capgemini.fs.app.ekms.service.ReplyService;

public class ReplyServiceImpl implements ReplyService {
	PersistenceService persistenceService = null;

	public PersistenceService getPersistenceService() {
		return persistenceService;
	}

	public void setPersistenceService(PersistenceService persistenceService) {
		this.persistenceService = persistenceService;
	}

	public void createReply(Reply reply) {
		getReplyDAO().save(reply);
	}

	public void createReply(Reply reply, User author, Topic topic) {
		author = getPersistenceService().getUserDAO().get(User.class,
				author.getId());
		topic = getPersistenceService().getTopicDAO().get(Topic.class,
				topic.getId());
		reply.setAuthor(author);
		reply.setTopic(topic);
		author.getReplies().add(reply);
		topic.getReplies().add(reply);
		createReply(reply);
		getPersistenceService().getUserDAO().update(author);
		getPersistenceService().getTopicDAO().update(topic);

	}

	public ReplyDAO getReplyDAO() {
		return getPersistenceService().getReplyDAO();
	}

}
